home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / 4.2bsd / connection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-13  |  25.5 KB  |  994 lines

  1. /***********************************************************
  2. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: connection.c,v 1.120 89/11/13 11:53:33 rws Exp $ */
  25. /*****************************************************************
  26.  *  Stuff to create connections --- OS dependent
  27.  *
  28.  *      EstablishNewConnections, CreateWellKnownSockets, ResetWellKnownSockets,
  29.  *      CloseDownConnection, CheckConnections, AddEnabledDevice,
  30.  *    RemoveEnabledDevice, OnlyListToOneClient,
  31.  *      ListenToAllClients,
  32.  *
  33.  *      (WaitForSomething is in its own file)
  34.  *
  35.  *      In this implementation, a client socket table is not kept.
  36.  *      Instead, what would be the index into the table is just the
  37.  *      file descriptor of the socket.  This won't work for if the
  38.  *      socket ids aren't small nums (0 - 2^8)
  39.  *
  40.  *****************************************************************/
  41.  
  42. #include <dbm.h>
  43. #undef NULL
  44. #include "X.h"
  45. #include "Xproto.h"
  46. #include <sys/param.h>
  47. #include <errno.h>
  48. #include "Xos.h"            /* for strings, file, time */
  49. #include <sys/socket.h>
  50.  
  51. #include <signal.h>
  52. #include <fcntl.h>
  53. #include <setjmp.h>
  54.  
  55. #ifdef hpux
  56. #include <sys/utsname.h>
  57. #include <sys/ioctl.h>
  58. #endif
  59.  
  60. #ifdef TCPCONN
  61. # include <netinet/in.h>
  62. # ifndef hpux
  63. #  ifdef apollo
  64. #   ifndef NO_TCP_H
  65. #    include <netinet/tcp.h>
  66. #   endif
  67. #  else
  68. #   include <netinet/tcp.h>
  69. #  endif
  70. # endif
  71. #endif
  72.  
  73. #ifdef UNIXCONN
  74. /*
  75.  * sites should be careful to have separate /tmp directories for diskless nodes
  76.  */
  77. #include <sys/un.h>
  78. #include <sys/stat.h>
  79. static int unixDomainConnection = -1;
  80. #endif
  81.  
  82. #include <stdio.h>
  83. #include <sys/uio.h>
  84. #include "osstruct.h"
  85. #include "osdep.h"
  86. #include "opaque.h"
  87. #include "dixstruct.h"
  88.  
  89. #ifdef DNETCONN
  90. #include <netdnet/dn.h>
  91. #endif /* DNETCONN */
  92.  
  93. typedef long CCID;      /* mask of indices into client socket table */
  94.  
  95. #ifndef X_UNIX_PATH
  96. #ifdef hpux
  97. #define X_UNIX_DIR    "/usr/spool/sockets/X11"
  98. #define X_UNIX_PATH    "/usr/spool/sockets/X11/"
  99. #define OLD_UNIX_DIR    "/tmp/.X11-unix"
  100. #else
  101. #define X_UNIX_DIR    "/tmp/.X11-unix"
  102. #define X_UNIX_PATH    "/tmp/.X11-unix/X"
  103. #endif
  104. #endif
  105.  
  106. char *display;            /* The display number */
  107. int lastfdesc;            /* maximum file descriptor */
  108.  
  109. long WellKnownConnections;    /* Listener mask */
  110. long EnabledDevices[mskcnt];    /* mask for input devices that are on */
  111. long AllSockets[mskcnt];    /* select on this */
  112. long AllClients[mskcnt];    /* available clients */
  113. long LastSelectMask[mskcnt];    /* mask returned from last select call */
  114. long ClientsWithInput[mskcnt];    /* clients with FULL requests in buffer */
  115. long ClientsWriteBlocked[mskcnt];/* clients who cannot receive output */
  116. long OutputPending[mskcnt];    /* clients with reply/event data ready to go */
  117. long MaxClients = MAXSOCKS ;
  118. long OutputBufferSize = BUFSIZ; /* output buffer size (must be > 0) */
  119. long NConnBitArrays = mskcnt;
  120. Bool NewOutputPending;        /* not yet attempted to write some new output */
  121. Bool AnyClientsWriteBlocked;    /* true if some client blocked on write */
  122.  
  123. static Bool SendSignal;        /* send SIGUSR1 to parent process */
  124. static int ParentProcess;
  125.  
  126. static Bool debug_conns = FALSE;
  127.  
  128. static int SavedAllClients[mskcnt];
  129. static int SavedAllSockets[mskcnt];
  130. static int SavedClientsWithInput[mskcnt];
  131. static int GrabInProgress = 0;
  132.  
  133. int ConnectionTranslation[MAXSOCKS];
  134. extern ClientPtr NextAvailableClient();
  135.  
  136. extern int AutoResetServer();
  137. extern int GiveUp();
  138. extern XID CheckAuthorization();
  139. static void CloseDownFileDescriptor(), ErrorConnMax();
  140. extern void FreeOsBuffers(), ResetOsBuffers();
  141.  
  142. #ifdef XDMCP
  143. void XdmcpOpenDisplay(), XdmcpInit(), XdmcpReset(), XdmcpCloseDisplay();
  144. #endif
  145.  
  146. #ifdef TCPCONN
  147. static int
  148. open_tcp_socket ()
  149. {
  150.     struct sockaddr_in insock;
  151.     int request;
  152.     int retry;
  153. #ifndef SO_DONTLINGER
  154. #ifdef SO_LINGER
  155.     static int linger[2] = { 0, 0 };
  156. #endif /* SO_LINGER */
  157. #endif /* SO_DONTLINGER */
  158.  
  159. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  160. #ifndef FORCE_DISPLAY_NUM
  161.     extern int AIXTCPSocket;
  162.     if (AIXTCPSocket>=0) {
  163.         request= AIXTCPSocket;
  164.     } else
  165. #endif /* FORCE_DISPLAY_NUM */
  166. #endif /* AIX && etc. */
  167.     if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0) 
  168.     {
  169.     Error ("Creating TCP socket");
  170.     return -1;
  171.     } 
  172. #ifdef SO_REUSEADDR
  173.     /* Necesary to restart the server without a reboot */
  174. #ifdef hpux
  175.     set_socket_option (request, SO_REUSEADDR);
  176. #else
  177.     {
  178.     int one = 1;
  179.     setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
  180.     }
  181. #endif
  182. #endif /* SO_REUSEADDR */
  183. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  184. #ifndef FORCE_DISPLAY_NUMBER
  185.     if (AIXTCPSocket<0)
  186. #endif
  187. #endif
  188.     {
  189.     bzero ((char *)&insock, sizeof (insock));
  190.     insock.sin_family = AF_INET;
  191.     insock.sin_port = htons ((unsigned short)(X_TCP_PORT + atoi (display)));
  192.     insock.sin_addr.s_addr = htonl(INADDR_ANY);
  193.     retry = 20;
  194.     while (bind(request, (struct sockaddr *) &insock, sizeof (insock)))
  195.     {
  196.     if (--retry == 0) {
  197.         Error ("Binding TCP socket");
  198.         close (request);
  199.         return -1;
  200.     }
  201. #ifdef SO_REUSEADDR
  202.     sleep (1);
  203. #else
  204.     sleep (10);
  205. #endif /* SO_REUSEDADDR */
  206.     }
  207.     }
  208. #ifdef SO_DONTLINGER
  209.     if(setsockopt (request, SOL_SOCKET, SO_DONTLINGER, (char *)NULL, 0))
  210.     Error ("Setting TCP SO_DONTLINGER");
  211. #else
  212. #ifdef SO_LINGER
  213.     if(setsockopt (request, SOL_SOCKET, SO_LINGER,
  214.            (char *)linger, sizeof(linger)))
  215.     Error ("Setting TCP SO_LINGER");
  216. #endif /* SO_LINGER */
  217. #endif /* SO_DONTLINGER */
  218.     if (listen (request, 5)) {
  219.     Error ("TCP Listening");
  220.     close (request);
  221.     return -1;
  222.     }
  223.     return request;
  224. }
  225. #endif /* TCPCONN */
  226.  
  227. #ifdef UNIXCONN
  228.  
  229. static struct sockaddr_un unsock;
  230.  
  231. static int
  232. open_unix_socket ()
  233. {
  234.     int oldUmask;
  235.     int request;
  236.  
  237.     unsock.sun_family = AF_UNIX;
  238.     oldUmask = umask (0);
  239. #ifdef X_UNIX_DIR
  240.     mkdir (X_UNIX_DIR, 0777);
  241.     chmod (X_UNIX_DIR, 0777);
  242. #endif
  243.     strcpy (unsock.sun_path, X_UNIX_PATH);
  244.     strcat (unsock.sun_path, display);
  245. #ifdef hpux
  246.     {  
  247.         /*    The following is for backwards compatibility
  248.          *    with old HP clients. This old scheme predates the use
  249.       *    of the /usr/spool/sockets directory, and uses hostname:display
  250.       *    in the /tmp/.X11-unix directory
  251.          */
  252.         struct utsname systemName;
  253.     static char oldLinkName[256];
  254.  
  255.         uname(&systemName);
  256.         strcpy(oldLinkName, OLD_UNIX_DIR);
  257.         mkdir(oldLinkName, 0777);
  258.         chown(oldLinkName, 2, 3);
  259.         strcat(oldLinkName, "/");
  260.         strcat(oldLinkName, systemName.nodename);
  261.         strcat(oldLinkName, display);
  262.         unlink(oldLinkName);
  263.         symlink(unsock.sun_path, oldLinkName);
  264.     }
  265. #endif    /* hpux */
  266.     unlink (unsock.sun_path);
  267.     if ((request = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) 
  268.     {
  269.     Error ("Creating Unix socket");
  270.     return -1;
  271.     } 
  272.     if (bind(request, (struct sockaddr *)&unsock, strlen(unsock.sun_path)+2))
  273.     {
  274.     Error ("Binding Unix socket");
  275.     close (request);
  276.     return -1;
  277.     }
  278.     if (listen (request, 5))
  279.     {
  280.     Error ("Unix Listening");
  281.     close (request);
  282.     return -1;
  283.     }
  284.     (void)umask(oldUmask);
  285.     return request;
  286. }
  287. #endif /*UNIXCONN */
  288.  
  289. #ifdef hpux
  290. /*
  291.  * hpux returns EOPNOTSUPP when using getpeername on a unix-domain
  292.  * socket.  In this case, smash the socket address with the address
  293.  * used to bind the connection socket and return success.
  294.  */
  295. hpux_getpeername(fd, from, fromlen)
  296.     int    fd;
  297.     struct sockaddr *from;
  298.     int            *fromlen;
  299. {
  300.     int        ret;
  301.     int        len;
  302.  
  303.     ret = getpeername(fd, from, fromlen);
  304.     if (ret == -1 && errno == EOPNOTSUPP)
  305.     {
  306.     ret = 0;
  307.     len = strlen(unsock.sun_path)+2;
  308.     if (len > *fromlen)
  309.         len = *fromlen;
  310.     bcopy ((char *) &unsock, (char *) from, len);
  311.     *fromlen = len;
  312.     }
  313.     return ret;
  314. }
  315.  
  316. #define getpeername(fd, from, fromlen)    hpux_getpeername(fd, from, fromlen)
  317.  
  318. #endif
  319.  
  320. #ifdef DNETCONN
  321. static int
  322. open_dnet_socket ()
  323. {
  324.     int request;
  325.     struct sockaddr_dn dnsock;
  326.  
  327.     if ((request = socket (AF_DECnet, SOCK_STREAM, 0)) < 0) 
  328.     {
  329.     Error ("Creating DECnet socket");
  330.     return -1;
  331.     } 
  332.     bzero ((char *)&dnsock, sizeof (dnsock));
  333.     dnsock.sdn_family = AF_DECnet;
  334.     sprintf(dnsock.sdn_objname, "X$X%d", atoi (display));
  335.     dnsock.sdn_objnamel = strlen(dnsock.sdn_objname);
  336.     if (bind (request, (struct sockaddr *) &dnsock, sizeof (dnsock)))
  337.     {
  338.     Error ("Binding DECnet socket");
  339.     close (request);
  340.     return -1;
  341.     }
  342.     if (listen (request, 5))
  343.     {
  344.     Error ("DECnet Listening");
  345.     close (request);
  346.     return -1;
  347.     }
  348.     return request;
  349. }
  350. #endif /* DNETCONN */
  351.  
  352. /*****************
  353.  * CreateWellKnownSockets
  354.  *    At initialization, create the sockets to listen on for new clients.
  355.  *****************/
  356.  
  357. void
  358. CreateWellKnownSockets()
  359. {
  360.     int        request, i;
  361.  
  362.     CLEARBITS(AllSockets);
  363.     CLEARBITS(AllClients);
  364.     CLEARBITS(LastSelectMask);
  365.     CLEARBITS(ClientsWithInput);
  366.  
  367.     for (i=0; i<MAXSOCKS; i++) ConnectionTranslation[i] = 0;
  368.     
  369. #ifdef    hpux
  370.     lastfdesc = _NFILE - 1;
  371. #else
  372.     lastfdesc = getdtablesize() - 1;
  373. #endif    /* hpux */
  374.  
  375.     if (lastfdesc > MAXSOCKS)
  376.     {
  377.     lastfdesc = MAXSOCKS;
  378.     if (debug_conns)
  379.         ErrorF( "GOT TO END OF SOCKETS %d\n", MAXSOCKS);
  380.     }
  381.  
  382.     WellKnownConnections = 0;
  383. #ifdef TCPCONN
  384.     if ((request = open_tcp_socket ()) != -1) {
  385.     WellKnownConnections |= (1L << request);
  386.     DefineSelf (request);
  387.     }
  388. #endif /* TCPCONN */
  389. #ifdef UNIXCONN
  390.     if ((request = open_unix_socket ()) != -1) {
  391.     WellKnownConnections |= (1L << request);
  392.     unixDomainConnection = request;
  393.     }
  394. #endif /* UNIXCONN */
  395. #ifdef DNETCONN
  396.     if ((request = open_dnet_socket ()) != -1) {
  397.     WellKnownConnections |= (1L << request);
  398.     DefineSelf (request);
  399.     }
  400. #endif /* DNETCONN */
  401.     if (WellKnownConnections == 0)
  402.         FatalError ("Cannot establish any listening sockets");
  403.     signal (SIGPIPE, SIG_IGN);
  404.     signal (SIGHUP, AutoResetServer);
  405.     signal (SIGINT, GiveUp);
  406.     signal (SIGTERM, GiveUp);
  407.     AllSockets[0] = WellKnownConnections;
  408.     ResetHosts(display);
  409.     /*
  410.      * Magic:  If SIGUSR1 was set to SIG_IGN when
  411.      * the server started, assume that either
  412.      *
  413.      *  a- The parent process is ignoring SIGUSR1
  414.      *
  415.      * or
  416.      *
  417.      *  b- The parent process is expecting a SIGUSR1
  418.      *     when the server is ready to accept connections
  419.      *
  420.      * In the first case, the signal will be harmless,
  421.      * in the second case, the signal will be quite
  422.      * useful
  423.      */
  424.     if (signal (SIGUSR1, SIG_IGN) == SIG_IGN)
  425.     SendSignal = TRUE;
  426.     ParentProcess = getppid ();
  427.     if (SendSignal) {
  428.     if (ParentProcess > 0) {
  429.         kill (ParentProcess, SIGUSR1);
  430.     }
  431.     }
  432. #ifdef XDMCP
  433.     XdmcpInit ();
  434. #endif
  435. }
  436.  
  437. void
  438. ResetWellKnownSockets ()
  439. {
  440.     ResetOsBuffers();
  441. #ifdef UNIXCONN
  442.     if (unixDomainConnection != -1)
  443.     {
  444.     /*
  445.      * see if the unix domain socket has disappeared
  446.      */
  447.     struct stat    statb;
  448.  
  449.     if (stat (unsock.sun_path, &statb) == -1 ||
  450.         (statb.st_mode & S_IFMT) != S_IFSOCK)
  451.     {
  452.         ErrorF ("Unix domain socket %s trashed, recreating\n",
  453.             unsock.sun_path);
  454.         (void) unlink (unsock.sun_path);
  455.         (void) close (unixDomainConnection);
  456.         WellKnownConnections &= ~(1L << unixDomainConnection);
  457.         unixDomainConnection = open_unix_socket ();
  458.         if (unixDomainConnection != -1)
  459.         WellKnownConnections |= (1L << unixDomainConnection);
  460.     }
  461.     }
  462. #endif /* UNIXCONN */
  463.     ResetAuthorization ();
  464.     ResetHosts(display);
  465.     /*
  466.      * See above in CreateWellKnownSockets about SIGUSR1
  467.      */
  468.     if (SendSignal) {
  469.     if (ParentProcess > 0) {
  470.         kill (ParentProcess, SIGUSR1);
  471.     }
  472.     }
  473.     /*
  474.      * restart XDMCP
  475.      */
  476. #ifdef XDMCP
  477.     XdmcpReset ();
  478. #endif
  479. }
  480.  
  481. /*****************************************************************
  482.  * ClientAuthorized
  483.  *
  484.  *    Sent by the client at connection setup:
  485.  *                typedef struct _xConnClientPrefix {
  486.  *                   CARD8    byteOrder;
  487.  *                   BYTE    pad;
  488.  *                   CARD16    majorVersion, minorVersion;
  489.  *                   CARD16    nbytesAuthProto;    
  490.  *                   CARD16    nbytesAuthString;   
  491.  *                 } xConnClientPrefix;
  492.  *
  493.  *         It is hoped that eventually one protocol will be agreed upon.  In the
  494.  *        mean time, a server that implements a different protocol than the
  495.  *        client expects, or a server that only implements the host-based
  496.  *        mechanism, will simply ignore this information.
  497.  *
  498.  *****************************************************************/
  499.  
  500. char * 
  501. ClientAuthorized(client, proto_n, auth_proto, string_n, auth_string)
  502.     ClientPtr client;
  503.     char *auth_proto, *auth_string;
  504.     unsigned short proto_n, string_n;
  505. {
  506.     register OsCommPtr priv;
  507.     union {
  508.     struct sockaddr sa;
  509. #ifdef UNIXCONN
  510.     struct sockaddr_un un;
  511. #endif /* UNIXCONN */
  512. #ifdef TCPCONN
  513.     struct sockaddr_in in;
  514. #endif /* TCPCONN */
  515. #ifdef DNETCONN
  516.     struct sockaddr_dn dn;
  517. #endif /* DNETCONN */
  518.     } from;
  519.     int    fromlen = sizeof (from);
  520.     XID     auth_id;
  521.  
  522.     auth_id = CheckAuthorization (proto_n, auth_proto,
  523.                   string_n, auth_string);
  524.  
  525.     priv = (OsCommPtr)client->osPrivate;
  526.     if (auth_id == (XID) ~0L && 
  527.        getpeername (priv->fd, &from.sa, &fromlen) != -1 &&
  528.         !InvalidHost (&from.sa, fromlen))
  529.     {
  530.     auth_id = (XID) 0;
  531.     }
  532.  
  533.     if (auth_id == (XID) ~0L)
  534.     return "Client is not authorized to connect to Server";
  535.  
  536.     priv->auth_id = auth_id;
  537.     priv->conn_time = 0;
  538.  
  539.     /* At this point, if the client is authorized to change the access control
  540.      * list, we should getpeername() information, and add the client to
  541.      * the selfhosts list.  It's not really the host machine, but the
  542.      * true purpose of the selfhosts list is to see who may change the
  543.      * access control list.
  544.      */
  545.     return((char *)NULL);
  546. }
  547.  
  548. /*****************
  549.  * EstablishNewConnections
  550.  *    If anyone is waiting on listened sockets, accept them.
  551.  *    Returns a mask with indices of new clients.  Updates AllClients
  552.  *    and AllSockets.
  553.  *****************/
  554.  
  555. void
  556. EstablishNewConnections()
  557. {
  558.     long readyconnections;     /* mask of listeners that are ready */
  559.     int curconn;                  /* fd of listener that's ready */
  560.     register int newconn;         /* fd of new client */
  561.     long connect_time;
  562.     register int i;
  563.     register ClientPtr client;
  564.     register OsCommPtr oc;
  565.  
  566. #ifdef TCP_NODELAY
  567.     union {
  568.     struct sockaddr sa;
  569. #ifdef UNIXCONN
  570.     struct sockaddr_un un;
  571. #endif /* UNIXCONN */
  572. #ifdef TCPCONN
  573.     struct sockaddr_in in;
  574. #endif /* TCPCONN */
  575. #ifdef DNETCONN
  576.     struct sockaddr_dn dn;
  577. #endif /* DNETCONN */
  578.     } from;
  579.     int    fromlen;
  580. #endif /* TCP_NODELAY */
  581.  
  582.     readyconnections = (LastSelectMask[0] & WellKnownConnections);
  583.     if (!readyconnections)
  584.     return;
  585.     connect_time = GetTimeInMillis();
  586.     /* kill off stragglers */
  587.     for (i=1; i<currentMaxClients; i++)
  588.     {
  589.     if (client = clients[i])
  590.     {
  591.         oc = (OsCommPtr)(client->osPrivate);
  592.         if (oc && (oc->conn_time != 0) &&
  593.         (connect_time - oc->conn_time) >= TimeOutValue)
  594.         CloseDownClient(client);     
  595.     }
  596.     }
  597.     while (readyconnections) 
  598.     {
  599.     curconn = ffs (readyconnections) - 1;
  600.     readyconnections &= ~(1 << curconn);
  601.     if ((newconn = accept (curconn,
  602.                   (struct sockaddr *) NULL, 
  603.                   (int *)NULL)) < 0) 
  604.         continue;
  605. #ifdef TCP_NODELAY
  606.     fromlen = sizeof (from);
  607.     if (!getpeername (newconn, &from.sa, &fromlen))
  608.     {
  609.         if (fromlen && (from.sa.sa_family == AF_INET)) 
  610.         {
  611.         int mi = 1;
  612.         setsockopt (newconn, IPPROTO_TCP, TCP_NODELAY,
  613.                (char *)&mi, sizeof (int));
  614.         }
  615.     }
  616. #endif /* TCP_NODELAY */
  617. #ifdef    hpux
  618.     /*
  619.      * HPUX does not have  FNDELAY
  620.      */
  621.     {
  622.         int    arg;
  623.         arg = 1;
  624.         ioctl(newconn, FIOSNBIO, &arg);
  625.     }
  626. #else
  627.     fcntl (newconn, F_SETFL, FNDELAY);
  628. #endif /* hpux */
  629.     oc = (OsCommPtr)xalloc(sizeof(OsCommRec));
  630.     if (!oc)
  631.     {
  632.         xfree(oc);
  633.         ErrorConnMax(newconn);
  634.         close(newconn);
  635.         continue;
  636.     }
  637.     if (GrabInProgress)
  638.     {
  639.         BITSET(SavedAllClients, newconn);
  640.         BITSET(SavedAllSockets, newconn);
  641.     }
  642.     else
  643.     {
  644.         BITSET(AllClients, newconn);
  645.         BITSET(AllSockets, newconn);
  646.     }
  647.     oc->fd = newconn;
  648.     oc->input = (ConnectionInputPtr)NULL;
  649.     oc->output = (ConnectionOutputPtr)NULL;
  650.     oc->conn_time = connect_time;
  651.     if ((newconn < lastfdesc) &&
  652.         (client = NextAvailableClient((pointer)oc)))
  653.     {
  654.         ConnectionTranslation[newconn] = client->index;
  655.     }
  656.     else
  657.     {
  658.         ErrorConnMax(newconn);
  659.         CloseDownFileDescriptor(oc);
  660.     }
  661. #ifdef XDMCP
  662.     /* indicate to Xdmcp protocol that we've opened new client */
  663.     XdmcpOpenDisplay(newconn);
  664. #endif /* XDMCP */
  665.     }
  666. }
  667.  
  668. #define NOROOM "Maximum number of clients reached"
  669.  
  670. /************
  671.  *   ErrorConnMax
  672.  *     Fail a connection due to lack of client or file descriptor space
  673.  ************/
  674.  
  675. static void
  676. ErrorConnMax(fd)
  677.     register int fd;
  678. {
  679.     xConnSetupPrefix csp;
  680.     char pad[3];
  681.     struct iovec iov[3];
  682.     char byteOrder = 0;
  683.     int whichbyte = 1;
  684.     struct timeval waittime;
  685.     long mask[mskcnt];
  686.  
  687.     /* if these seems like a lot of trouble to go to, it probably is */
  688.     waittime.tv_sec = BOTIMEOUT / MILLI_PER_SECOND;
  689.     waittime.tv_usec = (BOTIMEOUT % MILLI_PER_SECOND) *
  690.                (1000000 / MILLI_PER_SECOND);
  691.     CLEARBITS(mask);
  692.     BITSET(mask, fd);
  693.     (void)select(fd + 1, (int *) mask, (int *) NULL, (int *) NULL, &waittime);
  694.     /* try to read the byte-order of the connection */
  695.     (void)read(fd, &byteOrder, 1);
  696.     if ((byteOrder == 'l') || (byteOrder == 'B'))
  697.     {
  698.     csp.success = xFalse;
  699.     csp.lengthReason = sizeof(NOROOM) - 1;
  700.     csp.length = (sizeof(NOROOM) + 2) >> 2;
  701.     csp.majorVersion = X_PROTOCOL;
  702.     csp.minorVersion = X_PROTOCOL_REVISION;
  703.     if (((*(char *) &whichbyte) && (byteOrder == 'B')) ||
  704.         (!(*(char *) &whichbyte) && (byteOrder == 'l')))
  705.     {
  706.         swaps(&csp.majorVersion, whichbyte);
  707.         swaps(&csp.minorVersion, whichbyte);
  708.         swaps(&csp.length, whichbyte);
  709.     }
  710.     iov[0].iov_len = sz_xConnSetupPrefix;
  711.     iov[0].iov_base = (char *) &csp;
  712.     iov[1].iov_len = csp.lengthReason;
  713.     iov[1].iov_base = NOROOM;
  714.     iov[2].iov_len = (4 - (csp.lengthReason & 3)) & 3;
  715.     iov[2].iov_base = pad;
  716.     (void)writev(fd, iov, 3);
  717.     }
  718. }
  719.  
  720. /************
  721.  *   CloseDownFileDescriptor:
  722.  *     Remove this file descriptor and it's I/O buffers, etc.
  723.  ************/
  724.  
  725. static void
  726. CloseDownFileDescriptor(oc)
  727.     register OsCommPtr oc;
  728. {
  729.     int connection = oc->fd;
  730.  
  731.     close(connection);
  732.     FreeOsBuffers(oc);
  733.     BITCLEAR(AllSockets, connection);
  734.     BITCLEAR(AllClients, connection);
  735.     BITCLEAR(ClientsWithInput, connection);
  736.     if (GrabInProgress)
  737.     {
  738.     BITCLEAR(SavedAllSockets, connection);
  739.     BITCLEAR(SavedAllClients, connection);
  740.     BITCLEAR(SavedClientsWithInput, connection);
  741.     }
  742.     BITCLEAR(ClientsWriteBlocked, connection);
  743.     if (!ANYSET(ClientsWriteBlocked))
  744.         AnyClientsWriteBlocked = FALSE;
  745.     BITCLEAR(OutputPending, connection);
  746.     xfree(oc);
  747. }
  748.  
  749. /*****************
  750.  * CheckConections
  751.  *    Some connection has died, go find which one and shut it down 
  752.  *    The file descriptor has been closed, but is still in AllClients.
  753.  *    If would truly be wonderful if select() would put the bogus
  754.  *    file descriptors in the exception mask, but nooooo.  So we have
  755.  *    to check each and every socket individually.
  756.  *****************/
  757.  
  758. void
  759. CheckConnections()
  760. {
  761.     long        mask;
  762.     long        tmask[mskcnt]; 
  763.     register int    curclient, curoff;
  764.     int            i;
  765.     struct timeval    notime;
  766.     int r;
  767.  
  768.     notime.tv_sec = 0;
  769.     notime.tv_usec = 0;
  770.  
  771.     for (i=0; i<mskcnt; i++)
  772.     {
  773.     mask = AllClients[i];
  774.         while (mask)
  775.         {
  776.         curoff = ffs (mask) - 1;
  777.          curclient = curoff + (i << 5);
  778.             CLEARBITS(tmask);
  779.             BITSET(tmask, curclient);
  780.             r = select (curclient + 1, (int *)tmask, (int *)NULL, (int *)NULL, 
  781.             ¬ime);
  782.             if (r < 0)
  783.         CloseDownClient(clients[ConnectionTranslation[curclient]]);
  784.         mask &= ~(1 << curoff);
  785.     }
  786.     }    
  787. }
  788.  
  789.  
  790. /*****************
  791.  * CloseDownConnection
  792.  *    Delete client from AllClients and free resources 
  793.  *****************/
  794.  
  795. CloseDownConnection(client)
  796.     ClientPtr client;
  797. {
  798.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  799.  
  800.     if (oc->output && oc->output->count)
  801.     FlushClient(client, oc, (char *)NULL, 0);
  802.     ConnectionTranslation[oc->fd] = 0;
  803. #ifdef XDMCP
  804.     XdmcpCloseDisplay(oc->fd);
  805. #endif
  806.     CloseDownFileDescriptor(oc);
  807.     client->osPrivate = (pointer)NULL;
  808. }
  809.  
  810.  
  811. AddEnabledDevice(fd)
  812.     int fd;
  813. {
  814.     BITSET(EnabledDevices, fd);
  815.     BITSET(AllSockets, fd);
  816. }
  817.  
  818.  
  819. RemoveEnabledDevice(fd)
  820.     int fd;
  821. {
  822.     BITCLEAR(EnabledDevices, fd);
  823.     BITCLEAR(AllSockets, fd);
  824. }
  825.  
  826. /*****************
  827.  * OnlyListenToOneClient:
  828.  *    Only accept requests from  one client.  Continue to handle new
  829.  *    connections, but don't take any protocol requests from the new
  830.  *    ones.  Note that if GrabInProgress is set, EstablishNewConnections
  831.  *    needs to put new clients into SavedAllSockets and SavedAllClients.
  832.  *    Note also that there is no timeout for this in the protocol.
  833.  *    This routine is "undone" by ListenToAllClients()
  834.  *****************/
  835.  
  836. OnlyListenToOneClient(client)
  837.     ClientPtr client;
  838. {
  839.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  840.     int connection = oc->fd;
  841.  
  842.     if (! GrabInProgress)
  843.     {
  844.     COPYBITS (ClientsWithInput, SavedClientsWithInput);
  845.         BITCLEAR (SavedClientsWithInput, connection);
  846.     if (GETBIT(ClientsWithInput, connection))
  847.     {
  848.         CLEARBITS(ClientsWithInput);        
  849.         BITSET(ClientsWithInput, connection);
  850.     }
  851.     else
  852.         {
  853.         CLEARBITS(ClientsWithInput);        
  854.     }
  855.     COPYBITS(AllSockets, SavedAllSockets);
  856.     COPYBITS(AllClients, SavedAllClients);
  857.  
  858.     UNSETBITS(AllSockets, AllClients);
  859.     BITSET(AllSockets, connection);
  860.     CLEARBITS(AllClients);
  861.     BITSET(AllClients, connection);
  862.     GrabInProgress = client->index;
  863.     }
  864. }
  865.  
  866. /****************
  867.  * ListenToAllClients:
  868.  *    Undoes OnlyListentToOneClient()
  869.  ****************/
  870.  
  871. ListenToAllClients()
  872. {
  873.     if (GrabInProgress)
  874.     {
  875.     ORBITS(AllSockets, AllSockets, SavedAllSockets);
  876.     ORBITS(AllClients, AllClients, SavedAllClients);
  877.     ORBITS(ClientsWithInput, ClientsWithInput, SavedClientsWithInput);
  878.     GrabInProgress = 0;
  879.     }    
  880. }
  881.  
  882. /****************
  883.  * IgnoreClient
  884.  *    Removes one client from input masks.
  885.  *    Must have cooresponding call to AttendClient.
  886.  ****************/
  887.  
  888. static long IgnoredClientsWithInput[mskcnt];
  889. static long IgnoredSavedClientsWithInput[mskcnt];
  890.  
  891. IgnoreClient (client)
  892.     ClientPtr    client;
  893. {
  894.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  895.     int connection = oc->fd;
  896.  
  897.     if (GETBIT (ClientsWithInput, connection))
  898.     BITSET(IgnoredClientsWithInput, connection);
  899.     else
  900.     BITCLEAR(IgnoredClientsWithInput, connection);
  901.     BITCLEAR(ClientsWithInput, connection);
  902.     BITCLEAR(AllSockets, connection);
  903.     BITCLEAR(AllClients, connection);
  904.     if (GrabInProgress)
  905.     {
  906.         if (GETBIT (SavedClientsWithInput, connection))
  907.         BITSET(IgnoredSavedClientsWithInput, connection);
  908.         else
  909.         BITCLEAR(IgnoredSavedClientsWithInput, connection);
  910.     BITCLEAR(SavedClientsWithInput, connection);
  911.     BITCLEAR(SavedAllSockets, connection);
  912.     BITCLEAR(SavedAllClients, connection);
  913.     }
  914. }
  915.  
  916. /****************
  917.  * AttendClient
  918.  *    Adds one client back into the input masks.
  919.  ****************/
  920.  
  921. AttendClient (client)
  922.     ClientPtr    client;
  923. {
  924.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  925.     int connection = oc->fd;
  926.  
  927.     if (!GrabInProgress || GrabInProgress == client->index)
  928.     {
  929.         BITSET(AllClients, connection);
  930.         BITSET(AllSockets, connection);
  931.         if (GETBIT (IgnoredClientsWithInput, connection))
  932.         BITSET(ClientsWithInput, connection);
  933.     }
  934.     else
  935.     {
  936.     BITSET(SavedAllClients, connection);
  937.     BITSET(SavedAllSockets, connection);
  938.     if (GETBIT(IgnoredSavedClientsWithInput, connection))
  939.         BITSET(SavedClientsWithInput, connection);
  940.     }
  941. }
  942.  
  943. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  944.  
  945. static int grabbingClient;
  946. static int reallyGrabbed;
  947.  
  948. /****************
  949. * DontListenToAnybody:
  950. *   Don't listen to requests from any clients. Continue to handle new
  951. *   connections, but don't take any protocol requests from anybody.
  952. *   We have to take care if there is already a grab in progress, though.
  953. *   Undone by PayAttentionToClientsAgain. We also have to be careful
  954. *   not to accept any more input from the currently dispatched client.
  955. *   we do this be telling dispatch it is time to yield.
  956.  
  957. *   We call this when the server loses access to the glass
  958. *   (user hot-keys away).  This looks like a grab by the 
  959. *   server itself, but gets a little tricky if there is already
  960. *   a grab in progress.
  961. ******************/
  962.  
  963. void
  964. DontListenToAnybody()
  965. {
  966.     if (!GrabInProgress) {
  967.     COPYBITS(ClientsWithInput, SavedClientsWithInput);
  968.     COPYBITS(AllSockets, SavedAllSockets);
  969.     COPYBITS(AllClients, SavedAllClients);
  970.     GrabInProgress = TRUE;
  971.     reallyGrabbed = FALSE;
  972.     } else {
  973.     reallyGrabbed = TRUE;
  974.     }
  975.     CLEARBITS(ClientsWithInput);
  976.     UNSETBITS(AllSockets, AllClients);
  977.     CLEARBITS(AllClients);
  978.     isItTimeToYield = TRUE;
  979. }
  980.  
  981. void
  982. PayAttentionToClientsAgain()
  983. {
  984.     if (reallyGrabbed) {
  985.     BITSET(AllSockets, grabbingClient);
  986.     BITSET(AllClients, grabbingClient);
  987.     } else {
  988.     ListenToAllClients();
  989.     }
  990.     reallyGrabbed = FALSE;
  991. }
  992.  
  993. #endif
  994.